﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsModel.BusinessObjects;
using WindowsFormsPresenter.RestaurantMenuNorm;
using WindowsFormsPresenter.RestaurantMenu;

namespace WindowsFormApplication.FormCategory
{
    public partial class frmRestaurantMenuNormList : frmBaseCategory, IRestaurantMenuNormsView, IRestaurantMenusView
    {
        private RestaurantMenuNormsPresenter _restaurantMenuNormsPresenter;
        private RestaurantMenusPresenter _restaurantMenusPresenter;

        private static int totalPage = 0;
        private static int currentPage = 0;

        public IList<RestaurantMenuModel> RestaurantMenus
        {
            set
            {
                var restaurantMenus = value;
                cboMenuID.DataSource = restaurantMenus;
                cboMenuID.DisplayMember = "MenuName";
                cboMenuID.ValueMember = "MenuID";
            }
        }

        public IList<RestaurantMenuNormModel> RestaurantMenuNorms
        {
            set
            {
                var restaurantMenuNorms = value;
                grdMain.DataSource = restaurantMenuNorms;
                for (int i = 0; i < grdMain.DisplayLayout.Bands[0].Columns.Count; i++) { grdMain.DisplayLayout.Bands[0].Columns[i].Hidden = true; }
                grdMain.DisplayLayout.Bands[0].Columns["MenuNormCode"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["MenuNormName"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["MenuID"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["Description"].Hidden = false;

                grdMain.DisplayLayout.Bands[0].Columns["MenuNormCode"].Header.Caption = "Mã định mức";
                grdMain.DisplayLayout.Bands[0].Columns["MenuNormName"].Header.Caption = "Tên định mức";
                grdMain.DisplayLayout.Bands[0].Columns["MenuID"].Header.Caption = "Tên thực đơn";
                grdMain.DisplayLayout.Bands[0].Columns["Description"].Header.Caption = "Mô tả";

                grdMain.DisplayLayout.Bands[0].Columns["MenuID"].EditorComponent = cboMenuID;
                grdMain.DisplayLayout.Bands[0].Columns["MenuID"].CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;
            }
        }

        protected override void InitData()
        {
            try
            {
                _restaurantMenusPresenter.DisplayActiveByBranch();

                totalPage = _restaurantMenuNormsPresenter.GetTotalPageByBranch();
                _restaurantMenuNormsPresenter.DisplayPagedByBranch(currentPage);
                toolbarsManager.Tools["lblPage"].SharedProps.Caption = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void NextPage()
        {
            if (currentPage < totalPage)
            {
                currentPage = currentPage + 1;
                InitData();
            }
        }

        protected override void PreviousPage()
        {
            if (currentPage > 0)
            {
                currentPage = currentPage - 1;
                InitData();
            }
        }

        protected override void DeleteRow()
        {
            try
            {
                if (grdMain.ActiveRow.Index >= 0)
                {
                    string menuNormId = grdMain.ActiveRow.Cells[TablePrimaryKey].Value.ToString();
                    new RestaurantMenuNormPresenter(null).Delete(menuNormId);
                    currentPage = 0;
                    ShowMessageBox("Xóa dữ liệu hoàn thành !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowMessageBox("Bạn chưa chọn dòng để xóa !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public frmRestaurantMenuNormList()
        {
            InitializeComponent();

            _restaurantMenuNormsPresenter = new RestaurantMenuNormsPresenter(this);
            _restaurantMenusPresenter = new RestaurantMenusPresenter(this);
        }
    }
}
